﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShowFPS : MonoBehaviour {

    private float time_delta = 0.5f;//固定时间间隔
    private float last_time = 0;//上次统计fps时间
    private float fps = 0;//fps值
    private int frames = 0;//刷新的帧率


    private GUIStyle style;
    private void Awake()
    {
        Application.targetFrameRate = 60;//目标60fps
    }

    void Start () {
        last_time = Time.realtimeSinceStartup;//从游戏运行开始到现在的时间
        style = new GUIStyle();
        style.fontSize = 15;
        style.normal.textColor = Color.white;
	}

    private void OnGUI()
    {
        GUI.Label(new Rect(0, Screen.height - 20, 200, 200), "FPS:" + fps.ToString("f2"), style);
    }


    void Update () {
        ++frames;
        if (Time.realtimeSinceStartup >= last_time + time_delta)
        {
            fps = ((float)frames) / (Time.realtimeSinceStartup - last_time);//帧数/时间
            last_time = Time.realtimeSinceStartup;
            frames = 0;
        }
	}
}
